package com.cr.front.cost.service.impl;

import com.cr.front.cost.pojo.dto.param.CalCostParam;
import com.cr.front.cost.pojo.vo.CostVO;
import com.cr.front.cost.service.CostService;
import com.cr.front.vehicle.pojo.vo.VehicleVO;
import com.cr.front.vehicle.service.VehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class CostServiceImpl implements CostService {
    @Autowired
    private VehicleService vehicleService;
    @Override
    public CostVO  calculateRentCost(CalCostParam costParam) {
        VehicleVO vehicleVO = vehicleService.getDetail(costParam.getVehicleId());
        Float cost = vehicleVO.getDayRentPrice();
        BigDecimal bigDecimalCost = new BigDecimal(cost);
        String format = "yyyy-MM-dd";
        // 创建SimpleDateFormat对象
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long daysBetween = 0;
        try{
            Date d1 = sdf.parse(costParam.getStartDate());
            Date d2 = sdf.parse(costParam.getEndDate());

            daysBetween = daysBetween(d1,d2) + 1L;
        } catch (Exception e) {
            e.printStackTrace();
        }
        BigDecimal bigDecimalDays = new BigDecimal(daysBetween);
        BigDecimal TotalCost = bigDecimalCost.multiply(bigDecimalDays);
        CostVO costVO = new CostVO();
        costVO.setOriginalCost(TotalCost);
        costVO.setDiscountCost(TotalCost);
        if(daysBetween >= 15){
            BigDecimal bigDecimalDiscount = new BigDecimal("0.7");
            costVO.setDiscountCost(bigDecimalCost.multiply(bigDecimalDays).multiply(bigDecimalDiscount));
        }
        return costVO;


    }

    @Override
    public BigDecimal calculateLongRentCost(CalCostParam costParam) {
        VehicleVO vehicleVO = vehicleService.getDetail(costParam.getVehicleId());
        Float cost = vehicleVO.getDayRentPrice();
        //长租优惠
        BigDecimal bigDecimalDiscount = new BigDecimal("0.7");
        BigDecimal bigDecimalCost = new BigDecimal(cost);
        String format = "yyyy-MM-dd";
        // 创建SimpleDateFormat对象
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long daysBetween = 0;
        try{
            Date d1 = sdf.parse(costParam.getStartDate());
            Date d2 = sdf.parse(costParam.getEndDate());

            daysBetween = daysBetween(d1,d2)+ 1L;
        } catch (Exception e) {
            e.printStackTrace();
        }

        BigDecimal bigDecimalDays = new BigDecimal(daysBetween);
        return bigDecimalCost.multiply(bigDecimalDays).multiply(bigDecimalDiscount);
    }

    public static long daysBetween(Date date1, Date date2) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);

        long timeDiff = Math.abs(cal2.getTimeInMillis() - cal1.getTimeInMillis());
        return TimeUnit.DAYS.convert(timeDiff, TimeUnit.MILLISECONDS);
    }
}
